using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// Author:SQB
    /// </summary>
    /// 



    public partial class GenSubCategoryEntry : BasePage_SOM
    {

        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140808";
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;
            if (!IsPostBack)
            {
                SetLanguage();
                SetPageLoad();
            }
        }


        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
        }

        /// <summary>
        /// Bind drop down list gen Category
        /// </summary>
        private void BindGenCategory()
        {
            DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDGenCategory.GetGenCategory(this.LangCode, "", false);

            this.ddlGenCategory.Items.Clear();
            this.ddlGenCategory.DataSource = dt;
            this.ddlGenCategory.DataValueField = "GenCategoryID";
            this.ddlGenCategory.DataTextField = "Description";
            this.ddlGenCategory.DataBind();
            //ddlWorkCategory.Items.Insert(0, LiBlank());
            this.ddlGenCategory.SelectedIndex = 0;

            dt.Dispose();
            dt = null;
        }

        private void SetPageLoad()
        {
            if (this.Request["GenSubCategoryID"] != null)
            {
                DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDGenSubCategory.GetGenSubCategoryByID(this.LangCode, int.Parse(this.Request["GenSubCategoryID"].ToString()));

                this.hidID.Value = dt.Rows[0]["GenSubCategoryID"].ToString();
                this.txtSubCatEN.Text = dt.Rows[0]["Description1"].ToString();
                this.txtSubCatCH.Text = dt.Rows[0]["Description2"].ToString();
                this.txtSubCatHK.Text = dt.Rows[0]["Description3"].ToString();

                this.chkVoid.Checked = bool.Parse(dt.Rows[0]["FlagObsolete"].ToString());
                this.ddlGenCategory.SelectedValue = dt.Rows[0]["GenCategoryID"].ToString();
                this.ddlGenCategory.Enabled = false;

                if (this.chkVoid.Checked == true)
                {
                    this.ddlGenCategory.Enabled = false;
                    this.txtSubCatCH.Enabled = false;
                    this.txtSubCatEN.Enabled = false;
                    this.txtSubCatHK.Enabled = false;
                    this.chkVoid.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnSaveAndNew.Enabled = false;
                    this.btnSaveAndReturn.Enabled = false;
                }

                dt.Dispose();
                dt = null;
            }

            this.btnSave.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            this.btnSaveAndNew.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            this.btnSaveAndReturn.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");

        }

        private void SetLanguage()
        {
            this.lbl_CodeSetupTitle.Text = this.GetLabel("SOM_CodeSetupGenSubCategoryEntry");
            this.lblGenCategory.Text = this.GetLabel("SOM_GenCategory", true);
            this.lblGenSubCategory.Text = this.GetLabel("SOM_GenSubCategory");
            this.lblSubCatEN.Text = this.GetLabel("SOM_GenSubCategoryDesc1", true);
            this.lblSubCatCH.Text = this.GetLabel("SOM_GenSubCategoryDesc2", true);
            this.lblSubCatHK.Text = this.GetLabel("SOM_GenSubCategoryDesc3", true);
            this.btnSave.Text = this.GetLabel("COM_Save");
            this.btnSaveAndReturn.Text = this.GetLabel("COM_SaveAndReturn");
            this.btnSaveAndNew.Text = this.GetLabel("SOM_SaveNew");
            this.btnCancel.Text = this.GetLabel("COM_Cancel");
            this.chkVoid.Text = this.GetLabel("SOM_Void", true);
            BindGenCategory();

        }

        private int Save(int genSubCategoryID, int action)
        {
            return MTRC.ODMS.DAL.SOM.CodeSetup.CDGenSubCategory.Update(
                                                                        genSubCategoryID,
                                                                        int.Parse(this.ddlGenCategory.SelectedValue),
                                                                        this.txtSubCatEN.Text.Trim(),
                                                                        this.txtSubCatCH.Text.Trim(),
                                                                        this.txtSubCatHK.Text.Trim(),
                                                                        this.chkVoid.Checked,
                                                                        this.ODMUser.ID,
                                                                        DateTime.Now,
                                                                        this.ODMUser.ID,
                                                                        action
                                                                        );
        }

        private void Clear()
        {
            this.txtSubCatCH.Text = "";
            this.txtSubCatEN.Text = "";
            this.txtSubCatHK.Text = "";

            this.ddlGenCategory.Enabled = true;
            this.ddlGenCategory.SelectedIndex = 0;
            this.chkVoid.Checked = false;
        }

        private void AfterVoid()
        {
            this.txtSubCatCH.Enabled = false;
            this.txtSubCatEN.Enabled = false;
            this.txtSubCatHK.Enabled = false;
            this.chkVoid.Enabled = false;
            this.btnSave.Enabled = false;
            this.btnSaveAndNew.Enabled = false;
            this.btnSaveAndReturn.Enabled = false;
        }


        private bool CheckUnique(bool insert)
        {
            if (insert == true)
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDGenSubCategory.CheckGenSubCategoryUnqiue(this.txtSubCatEN.Text.Trim(), 1, -1).Rows.Count > 0 && this.txtSubCatEN.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc1Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDGenSubCategory.CheckGenSubCategoryUnqiue(this.txtSubCatCH.Text.Trim(), 2, -1).Rows.Count > 0 && this.txtSubCatCH.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc2Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDGenSubCategory.CheckGenSubCategoryUnqiue(this.txtSubCatHK.Text.Trim(), 3, -1).Rows.Count > 0 && this.txtSubCatHK.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc3Unique"));
                    return false;
                }
                return true;
            }
            else
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDGenSubCategory.CheckGenSubCategoryUnqiue(this.txtSubCatEN.Text.Trim(), 1, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtSubCatEN.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc1Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDGenSubCategory.CheckGenSubCategoryUnqiue(this.txtSubCatCH.Text.Trim(), 2, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtSubCatCH.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc2Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDGenSubCategory.CheckGenSubCategoryUnqiue(this.txtSubCatHK.Text.Trim(), 3, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtSubCatHK.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc3Unique"));
                    return false;
                }
                return true;
            }
        }


        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int id;

            if (this.hidID.Value == String.Empty)
            {
                if (CheckUnique(true))
                {
                    id = Save(0, 1);
                    this.hidID.Value = id.ToString();
                    this.ddlGenCategory.Enabled = false;
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    if (this.chkVoid.Checked == true)
                    {
                        AfterVoid();
                    }
                }
            }
            else
            {
                if (CheckUnique(false))
                {
                    id = Save(int.Parse(this.hidID.Value), 2);
                    this.ddlGenCategory.Enabled = false;
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    if (this.chkVoid.Checked == true)
                    {
                        AfterVoid();
                    }
                }
            }
        }

        protected void btnSaveAndReturn_Click(object sender, EventArgs e)
        {
            int id;

            if (this.hidID.Value == String.Empty)
            {
                if (CheckUnique(true))
                {
                    id = Save(0, 1);
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    string script = "<script language=javascript>function btnChange_Click(){window.location.href='GenSubCategoryList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                    this.Page.RegisterStartupScript("aaa", script);
                }
                else
                {
                    return;
                }
            }
            else
            {
                id = Save(int.Parse(this.hidID.Value), 2);
                this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                string script = "<script language=javascript>function btnChange_Click(){window.location.href='GenSubCategoryList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                this.Page.RegisterStartupScript("aaa", script);
            }
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            int id;
            if (this.hidID.Value == String.Empty)
            {
                if (CheckUnique(true))
                {
                    id = Save(0, 1);
                    this.hidID.Value = "";
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    Clear();
                }
            }
            else
            {
                if (CheckUnique(false))
                {
                    id = Save(int.Parse(this.hidID.Value), 2);
                    this.hidID.Value = "";
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    Clear();
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("GenSubCategoryList.aspx");
        }

        #endregion

    }
}
